//
//  ViewController.m
//  自定义布局
//
//  Created by 耐克了解了 on 17/4/17.
//  Copyright © 2017年 耐克了解了. All rights reserved.
//

#import "ViewController.h"
#import "TWPhotoCell.h"
#import "TWCircleLayout.h"
#import "LineLayout.h"
#import "TWGridLayout.h"


static NSString *cellID = @"cell";

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) NSMutableArray *imageNames;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TWCircleLayout *layout = [[TWCircleLayout  alloc]init];
    
    CGFloat width = self.view.frame.size.width;
    CGRect frame = CGRectMake(0, 100, width, 300);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:layout];
    [self.view addSubview:collectionView];
   // [collectionView registerClass:[TWPhotoCell class] forCellWithReuseIdentifier:cellID];
    
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([TWPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:cellID];
    collectionView.dataSource = self;
    self.collectionView = collectionView;
    collectionView.delegate = self;
   // collectionView.pagingEnabled = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([self.collectionView.collectionViewLayout isKindOfClass:[LineLayout class]]) {
        TWCircleLayout *layout = [[TWCircleLayout alloc]init];
      [self.collectionView setCollectionViewLayout:layout animated:YES];
    }else{
      LineLayout *layout  =[[LineLayout alloc]init];
      layout.itemSize = CGSizeMake(150, 180);
      [self.collectionView setCollectionViewLayout:layout animated:YES];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageNames.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TWPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];

    cell.imageName = self.imageNames[indexPath.item];
    
//    NSInteger tag = 10;
//    
//    UILabel *label = [cell.contentView viewWithTag:tag];
//    if (label == nil) {
//        label = [[UILabel alloc]init];
//        label.tag = 10;
//        [cell.contentView addSubview:label];
//    }
//    label.text = [NSString stringWithFormat:@"%zd",indexPath.item];
    
  //  [label sizeToFit];
  return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.imageNames removeObjectAtIndex:indexPath.item];
    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
}

- (NSMutableArray *)imageNames
{
    if (!_imageNames) {
        _imageNames = [NSMutableArray array];
        for (int i = 0; i < 20; i++) {
            [_imageNames addObject:[NSString stringWithFormat:@"%zd",i + 1]];
        }
    }
    return _imageNames;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
